package main;

import java.util.ArrayList;
import java.util.List;

import br.cin.stadium.middleware.directory.ServerLocation;
import br.cin.stadium.middleware.util.Logger;

public class StadiumDirectoryServer {

	/**
	 * Maintain servers references.
	 */
	private List<ServerLocation> servers;

	/**
	 * StadiumDirectory constructor.
	 */
	public StadiumDirectoryServer() {
		servers = new ArrayList<ServerLocation>();
	}

	/**
	 * Register a new Server into ServerDirectory.
	 * 
	 * @param server New server location
	 * @throws Exception If an error occurred.
	 */
	public void register(ServerLocation server) throws Exception {
		
		if (servers.contains(server)) {
			throw new Exception("The server " + server + " already exists.");
		}
		servers.add(server);
		
		Logger.log("Server added - " + server);
		Logger.log("Servers registreded - " + servers);
	}

	/**
	 * Remove a server from Server Directory.
	 * 
	 * @param server Server to be removed.
	 * @throws Exception If an error occurred.
	 */
	public void unRegister(ServerLocation server) throws Exception {
		if (!servers.contains(server)) {
			throw new Exception("The server " + server + " does not exists.");
		}
		servers.remove(server);

		Logger.log("Server removed - " + server);
		Logger.log("Servers registreded - " + servers);
	}

	public ServerLocation[] getServers() {
		ServerLocation[] objs = new ServerLocation[servers.size()];
		return servers.toArray(objs);
	}
}